!-----------------------------------------------------------------------
!  Copyright (c) 2020 Feasa Enterprises Ltd.
!  Rev 3.0  August 2020
!
!  All Rights Reserved.  Reproduction, adaptation, or translation
!  without prior written permission is prohibited, except as allowed
!  under the copyright laws.
!
!-----------------------------------------------------------------------
!
! Device        : Feasa_20i
! Manufacturer  : Feasa Enterprises Ltd.
! Description   : 20 Channel device to interrogate a LED
! Package       : 20 pin
! Test Platform : 3070
! Safeguard     : cmos_3v3
!
!-----------------------------------------------------------------------
! WARNING:  This is a Led Analyser function referenced by a PDL.
!
!   This test is not intended to be used directly for a Led analyser.
!   Instead, a part description library for the Led analyser should be
!   used where the part description language of that file refers to
!   this file as the part number of one of it's several "sub-components" .
!   Notice also that this test uses signal names rather than ordinal
!   pin numbers.  The referencing part description library should map
!   the device's actual pin numbers to these signal names.
!
!
! Additional Comments:
!
! This test captures the Intensity data of the LED using sequence capture mode
!
!-----------------------------------------------------------------------
!
!*******************************************************************************
!      Synchronous Serial Model for the Feasa LED Analyser
!
!Note: All commands sent to the Led Analyser use the following format
! To send a command
!       - Set Ser_in to MSB
!       - Set Ser_clk to [1]
!       - Set Ser_clk to [0]
!       - Set Ser_in to Bit 6
!       - Set Ser_clk to [1]
!       - Set Ser_clk to [0]
!       - Continue until all 8 bits have been clocked into the Led Analyser

! To read a byte
!       - Set Ser_clk to [1]
!       - Set Ser_clk to [0]
!       - Read Ser_out to MSB
!       - Set Ser_clk to [1]
!       - Set Ser_clk to [0]
!       - Read Ser_out Bit 6
!       - Continue until all 8 bits have been read from the Led Analyser

!
! - Execute the capture command by sending 0x45 to the Led Analyser
! - Check that the RyBy_bar output goes high and then low
! - Read back the Pre Capture Acknowledge (0XD4) from the Led Analyser
! - Wait unitl the RyBy_bar signal goes low to indicate that the capture
!   is complete.
! - Read back the post capture acknowledge by sending (0XBB)
!   and reading the positive response (0XD5)


test digital

vector cycle  20u      !minimum time 9u
receive delay 19u      !minimum time 8u

test time 10000m        !allow time to capture the data

set terminators to on
assign Ser_out            to pins "Ser_out"
assign Ser_in             to pins "Ser_in"

assign Ser_clk            to pins "Ser_clk"

assign GND1               to pins "GND1"

assign Reset              to pins "Reset"

assign OE_bar             to pins "OE_bar"
assign Ext_trig           to pins "Ext_trig"

assign PWM                to pins "PWM"

assign Addr_0             to pins "Addr_0"
assign Addr_1             to pins "Addr_1"
assign Addr_2             to pins "Addr_2"
assign Addr_3             to pins "Addr_3"
assign Addr_4             to pins "Addr_4"

assign WE_bar             to pins "WE_bar"
assign Int                to pins "Int"
assign Color              to pins "Color"
assign Sat                to pins "Sat"

assign RyBy_bar           to pins "RyBy_bar"

assign VCC                to pins "VCC"
assign GND                to pins "GND"

power  VCC, GND, GND1

family   CMOS_3V3

inputs   Ser_in, Ser_clk, Addr_0
outputs  Ser_out, RyBy_bar

nondigital Int, Color, Sat, Reset, OE_bar, Ext_trig, PWM
nondigital Addr_1, Addr_2, Addr_3, Addr_4, WE_bar


!********** assignments **********************


!*******************   Disable information        *****************

disable Ser_out with Addr_0 to "0"

set load on groups  Ser_out  to pull up
set ref on groups Addr_0,Ser_clk,Ser_in to dh 3.3, dl 0, rh 2, rl 1
set slew rate on groups Addr_0,Ser_clk,Ser_in to 50

!*******************************************************************
!*******************      VECTOR SECTION       *********************
!*******************************************************************


vector  Set_values     !set LED Analyser to known state
   set Addr_0          to "0"
   set Ser_clk         to "0"
   set Ser_in          to "0"
end vector

vector Keep_values     !Save input values.
   set Addr_0          to "k"
   set Ser_clk         to "k"
   set Ser_in          to "k"
end vector


vector Addr_0_high
   initialize to Keep_values
   set Addr_0          to "1"
end vector

vector Addr_0_low
   initialize to Keep_values
   set Addr_0          to "0"
end vector

vector Clock
   initialize to Keep_values
   set Ser_clk         to "t"
end vector

vector Clock_1
   initialize to Keep_values
   set Ser_clk         to "1"
end vector

vector Clock_0
   initialize to Keep_values
   set Ser_clk         to "0"
end vector

vector Start_bit
   initialize to Keep_values
   set Ser_in          to "1"
end vector

vector Ser_in_X
   initialize to Keep_values
   set Ser_in          to "x"
end vector

vector Ser_in_0
   initialize to Keep_values
   set Ser_in          to "0"
end vector

vector In_0
   initialize to Keep_values
   set Ser_in          to "0"
end vector

vector Ser_in_1
   initialize to Keep_values
   set Ser_in          to "1"
end vector

vector In_1
   initialize to Keep_values
   set Ser_in          to "1"
end vector

vector Ser_out_0
   initialize to Keep_values
   set Ser_out         to "0"
end vector

vector Out_0
   initialize to Keep_values
   set Ser_out         to "0"
end vector

vector Ser_out_1
   initialize to Keep_values
   set Ser_out         to "1"
end vector

vector Out_1
   initialize to Keep_values
   set Ser_out         to "1"
end vector

vector  Ser_out_x
   initialize               to Keep_values
   set   Ser_out            to "x"
end vector

vector  RyBy_bar_0
   initialize               to Keep_values
   set   RyBy_bar           to "0"
end vector

vector  RyBy_bar_1
   initialize               to Keep_values
   set   RyBy_bar           to "1"
end vector


!*******************************************************************
!*******************    SUBROUTINE SECTION     *********************
!*******************************************************************


sub Capture                      !send sequence capture command to LA - 0x45
   execute Addr_0_high                 !Chip Select = 1
   execute Clock_0
   execute Ser_in_0                    !0      send 01000101  0x45
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock

end sub


sub Pre_capture_acknowledge             !pre capture acknowledge
   execute Addr_0_high                  !Chip Select = 1
   execute Clock_0
   execute Clock
   execute Clock
   execute Ser_out_1                    !1  receive 11010100  0xD5
   execute Clock
   execute Clock
   execute Ser_out_1                    !1
   execute Clock
   execute Clock
   execute Ser_out_0                    !0
   execute Clock
   execute Clock
   execute Ser_out_1                    !1
   execute Clock
   execute Clock
   execute Ser_out_0                    !0
   execute Clock
   execute Clock
   execute Ser_out_1                    !1
   execute Clock
   execute Clock
   execute Ser_out_0                    !0
   execute Clock
   execute Clock
   execute Ser_out_1                    !0

end sub

sub Post_capture_acknowledge           !capture acknowledge
   execute Addr_0_high                 !Chip Select = 1
   execute Clock_0
   execute Ser_in_1                    !1      send 10111011  0xBB
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock

!wait 20u
!read back data from Ser_out
   execute Clock
   execute Clock
   execute Ser_out_1                    !1
   execute Clock
   execute Clock
   execute Ser_out_1                    !1
   execute Clock
   execute Clock
   execute Ser_out_0                    !0
   execute Clock
   execute Clock
   execute Ser_out_1                    !1
   execute Clock
   execute Clock
   execute Ser_out_0                    !0
   execute Clock
   execute Clock
   execute Ser_out_1                    !1
   execute Clock
   execute Clock
   execute Ser_out_0                    !0
   execute Clock
   execute Clock
   execute Ser_out_1                    !1
end sub


sub Initialize_Capture    !Initialize Capture - send 10101010 0xAA to LA
   execute Addr_0_high
   execute Clock_0
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
end sub

sub Reset_Comms              !Reset Communications- send 11111111 0xFF to LA
   execute Addr_0_low                  !Chip Select = 0
   execute Clock_0
   execute Ser_in_1                    !bit 7 = 1
   execute Clock
   execute Clock
   execute Ser_in_1                    !bit 6 = 1
   execute Clock
   execute Clock
   execute Ser_in_1                    !bit 5 = 1
   execute Clock
   execute Clock
   execute Ser_in_1                    !bit 4 = 1
   execute Clock
   execute Clock
   execute Ser_in_1                    !bit 3 = 1
   execute Clock
   execute Clock
   execute Ser_in_1                    !bit 2 = 1
   execute Clock
   execute Clock
   execute Ser_in_1                    !bit 1 = 1
   execute Clock
   execute Clock
   execute Ser_in_1                    !bit 0 = 1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Addr_0_high                 !Chip Select = 1
   wait 1m
end sub


!*******************************************************************
!*********************      UNIT SECTION       *********************
!*******************************************************************

!-------------------------------------------------------------------
!  Initialize all the inputs to a known value
!-------------------------------------------------------------------
unit "Initialize All"
    execute Set_values
end unit
!-------------------------------------------------------------------
!  Clear the input buffer of the Led Analyser to
!  ensure that the led analyser has detected no false
!  clock inputs
!-------------------------------------------------------------------
unit "Reset Communications"
        call Reset_Comms            !Send Reset Code x0FF
end unit
!-------------------------------------------------------------------
!   Unit to Initialize the Color Capture
!   Sends AA to the Led Analyser
!-------------------------------------------------------------------
unit "Initialize the LED Analyser"
        call Initialize_Capture   !Send Initialize Code x0AA
        homingloop 2000 times
          execute RyBy_bar_0 exit if pass
        end homingloop
        execute Addr_0_low                 !Addr_0=0
end unit

!-------------------------------------------------------------------
!   Unit to Capture and store the sequence Intensity data for each fiber.
!   Sends 0x45 to the Led Analyser
!   Analyser returns the pre-capture acknowledge D5
!   Ser_out is high whicle capture is in progress
!   After Capture Send Post capture request BB to LA
!   Analyser responds with Post capture acknowledge D5
!-------------------------------------------------------------------
unit "Capture Sequence"
      call Capture                            !Send Code 0x45
      call Pre_capture_acknowledge            !read 0xD5 to confirm

      homingloop 100000 times
         execute RyBy_bar_0 exit if pass      !wait until RYBY_bar goes low
      end homingloop

      call Post_capture_acknowledge           !Send 0xBB read 0xD5 to confirm
      execute Addr_0_low                      !Addr_0=0
end unit


!
! End of test
!


